﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using TimKen.App;
using TimKen.DataEntity;
using TimKen.BussLogic;
using System.Globalization;

namespace TimKen.Web.User
{
    public partial class Login : WebPage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(Request["UserName"]) && !string.IsNullOrEmpty(Request["Password"]))
            {
                string username = string.IsNullOrEmpty(Request["UserName"]) ? "" : Request["UserName"];
                string password = string.IsNullOrEmpty(Request["Password"]) ? "" : Request["Password"];
                string codestring = string.IsNullOrEmpty(Request["TxtCode"]) ? "" : Request["TxtCode"];
                Session["intcode"] = Session["intcode"] == null ? 0 : Convert.ToInt32(Session["intcode"]) + 1;
                if (Convert.ToInt32(Request["intcode"]) >= 3)
                {
                    if (codestring != "" && codestring != GetLangString("YourCode"))
                    {
                        if (!Session["CheckCode"].ToString().ToUpper().Trim().Equals(codestring.ToUpper()))
                        {
                            Alert(GetLangString("x_CodeNotAllowBlank"), "/Index.aspx");
                            //验证码验证失败divMessageBox     
                        }
                    }
                    else
                    {
                        Alert(GetLangString("x_CodeNotAllowBlank"), "/Index.aspx");
                    }
                }
                UserS u = UserFacade.Login(username, password);

                if (u != null && u.UserType != UserType.DataAdministrator && u.UserType != UserType.SystemAdministrator)
                {
                    Session.Add("tempUserName", username);
                    Session["User"] = u;
                    if (!u.IsValid)
                    {                  //盘点是否邮箱验证完成
                        Response.Redirect("/User/AfterRegister.aspx?action=1", false);
                    }
                    else
                    {
                        if (Request.QueryString["reurl"] != null)
                        {
                            if (!UserFacade.CheckUserPasswordExpired(u))
                            {
                                Response.Redirect("/User/UserAccountEdit.aspx?type=1");
                            }
                            else
                                Response.Redirect(Request.QueryString["reurl"]); //"/Product/Category.aspx");
                        }
                        else
                        {
                            if (u.UserType == UserType.SelfRegistered)
                                Response.Redirect("/Index.aspx", true);


                            if (!UserFacade.CheckUserPasswordExpired(u))
                            {
                                Response.Redirect("/User/UserAccountEdit.aspx?type=1");
                            }

                            else
                            {
                                if (Request.Path.Trim().ToLower() == "/Index.aspx")    // "/User/login.aspx"
                                {
                                    Response.Redirect("/Index.aspx");
                                    //if (u.UserType == UserType.FactoryFinance)
                                    //    Response.Redirect("/Order/CheckAccount.aspx");
                                    //else
                                    //    Response.Redirect("/Order/OrderList.aspx");
                                }
                                else
                                {
                                    Response.Write("1");
                                    //  Response.Redirect(Request.Path);
                                }
                            }
                        }
                    }
                }
                else
                {
                    Alert(GetLangString("ErrorLogin"), "/Index.aspx");
                }
            }
            else if (!string.IsNullOrEmpty(Request["language"]))//切换中英文
            {
                string language = string.IsNullOrEmpty(Request["language"]) ? "zh-CN" : Request["language"];
                Session.Remove("lan");
                Session.Add("lan", language);
                HttpCookie cookie = new HttpCookie("Language");
                DateTime dt = DateTime.Now;//定义时间对象
                TimeSpan ts = new TimeSpan(0, 20, 0);

                cookie.Expires.Add(ts);
                cookie.Value = language;
                HttpContext.Current.Response.AppendCookie(cookie);
                CultureInfo culture = new CultureInfo(language);
                System.Threading.Thread.CurrentThread.CurrentUICulture = culture;
                System.Threading.Thread.CurrentThread.CurrentCulture = culture;

                string oldurl = Session["pathUrl"] != null ? (Session["pathUrl"].ToString()) : "/Index.aspx";
                Session["pathUrl"] = oldurl;
                if (oldurl == "/User/Login.aspx?language=en-US" || oldurl == "/User/Login.aspx?language=zh-CN")
                    ///User/Login.aspx?language=en-US
                    oldurl = "/Index.aspx";
                Server.Transfer(oldurl);
                //Server.Transfer("/Index.aspx");
                // Server.Transfer(Request.Path);

                //Thread.CurrentThread.CurrentUICulture = CultureInfo.CreateSpecificCulture("zh-cn");
            }
        }
    }
}